//
//  Setting.m
//  ShareElectricity
//
//  Created by sunsoft on 2017/6/7.
//  Copyright © 2017年 sunnsoft.HH. All rights reserved.
//

#import "Setting.h"
#import "RechargeSuccessCell.h"
#import "WebViewVC.h"


@interface Setting ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)NSBundle *bundle;
@property (nonatomic,strong)UITableView * tableView;
@property (nonatomic,strong)NSArray * cellTitles;
@property (nonatomic,strong)NSArray * EcellTitles;
@property (nonatomic,strong)HNaviBar * bar;
@property (nonatomic,strong)UIButton * CBt;
@property (nonatomic,strong)UIButton * EBt;
@end

@implementation Setting

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.cellTitles = @[@"用戶協議", @"關於我們",@"私隱政策",@"語言"];
    self.EcellTitles = @[@"User Agreement", @"About Us",@"Prrvacy Policy",@"Language"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLanguage) name:@"changeLanguage" object:nil];
    self.view.backgroundColor = GrayColor;
    [SEChangeLanguage initUserLanguage];
    _bundle = [SEChangeLanguage bundle];
    __weak typeof(self) weakSelf = self;
   _bar = [HNaviBar customNaviBarWithTitle:[_bundle localizedStringForKey:@"設定" value:nil table:@"Localization"] andLeftEvent:^(UIButton *leftSender) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
        
    } aimView:self.view];
    [self initUI];
    
}
-(void)changeLanguage{
    
    _bar.titleLabel.text = [[SEChangeLanguage bundle] localizedStringForKey:@"設定" value:nil table:@"Localization"];
}
- (void)initUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, HHScreenWidth, HHScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = GrayColor;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[RechargeSuccessCell class] forCellReuseIdentifier:@"rechargeSuccessCell"];
    _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        WebViewVC * wvc = [[WebViewVC alloc] init];
        if (indexPath.row == 0) {
            
            wvc.Navtitle = @"用戶協議";
            [self.navigationController pushViewController:wvc animated:YES];
        }else if(indexPath.row == 1){
            
            wvc.Navtitle = @"關於我們";
            [self.navigationController pushViewController:wvc animated:YES];
        }else if(indexPath.row == 2){
            wvc.Navtitle = @"私隱政策";
            [self.navigationController pushViewController:wvc animated:YES];
        }
    }else{
        
        [[SEUserData sharedInstance] deletAccount];
        [SEUserData sharedInstance].user = nil;
        [self.navigationController popToRootViewControllerAnimated:YES];
        if ([[SEUserData sharedInstance].user.languageStr isEqualToString:@"ENG"]) {
            [SVProgressHUD showSuccessWithStatus:@"Account has log out"];
        }else{
            [SVProgressHUD showSuccessWithStatus:@"账号已退出"];
        }
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
     return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    switch (section) {
        case 0:
            return self.cellTitles.count;
            break;
            
        default:
            return 1;
            break;
    }
  
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RechargeSuccessCell * cell = [tableView dequeueReusableCellWithIdentifier:@"rechargeSuccessCell"];
    if (indexPath.section == 0) {
        
        NSString *title;
        if ([[SEUserData sharedInstance].user.languageStr isEqualToString:@"ENG"]) {
            title = self.EcellTitles[indexPath.row];
        }else{
            title = self.cellTitles[indexPath.row];
        }
//        cell.textLabel.text = [_bundle localizedStringForKey:title value:nil table:@"Localization"];
        cell.textLabel.text = title;
        if (indexPath.row != 3) {
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        }else{
            
            cell.lineView.hidden = YES;
            _CBt = [UIButton new];
            [_CBt setImage:[UIImage imageNamed:@"Login_N_select"] forState:UIControlStateNormal];
            [_CBt setImage:[UIImage imageNamed:@"Login_select-1"] forState:UIControlStateSelected];
            [_CBt setTitleColor:LabelColor forState:UIControlStateNormal];
            _CBt.titleLabel.font = HHFont(13);
            [_CBt setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
            [cell addSubview:_CBt];
            [_CBt mas_makeConstraints:^(MASConstraintMaker *make) {
               
                make.center.equalTo(cell);
                make.width.mas_offset(@80);
                
            }];
            [_CBt addTarget:self action:@selector(changeCHT:) forControlEvents:UIControlEventTouchUpInside];
            
            _EBt = [UIButton new];
            [_EBt setImage:[UIImage imageNamed:@"Login_N_select"] forState:UIControlStateNormal];
            [_EBt setImage:[UIImage imageNamed:@"Login_select-1"] forState:UIControlStateSelected];
            
            [_EBt setTitleColor:LabelColor forState:UIControlStateNormal];
            _EBt.titleLabel.font = HHFont(13);
            [_EBt setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
            [cell addSubview:_EBt];
            
            if ([[SEUserData sharedInstance].user.languageStr isEqualToString:@"ENG"]) {
                [_CBt setTitle:@"Chinese" forState:UIControlStateNormal];
                [_EBt setTitle:@"English" forState:UIControlStateNormal];
            }else{
                [_CBt setTitle:@"繁體" forState:UIControlStateNormal];
                [_EBt setTitle:@"英文" forState:UIControlStateNormal];
            }
            
            [_EBt mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.centerY.equalTo(cell);
                make.trailing.equalTo(cell).offset(-30);
                make.width.mas_offset(@80);
                
            }];
            [_EBt addTarget:self action:@selector(changeENG:) forControlEvents:UIControlEventTouchUpInside];
            if ([[SEUserData sharedInstance].user.languageStr isEqualToString:@"ENG"]) {
                _EBt.selected = YES;
            }else{
                _CBt.selected = YES;
            }
        }
        
    }else{
        
        cell.lineView.hidden = YES;
        cell.textLabel.textColor = RedColor;
        cell.textLabel.font = HHFont(15);
        if ([[SEUserData sharedInstance].user.languageStr isEqualToString:@"ENG"]) {
            cell.textLabel.text = @"Log Out";
        }else{
            cell.textLabel.text = @"登出";
        }
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    
    return 30;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * bgiew = [[UIView alloc] initWithFrame:CGRectMake(0, 0, HHScreenWidth, 10)];
    bgiew.backgroundColor = GrayColor;
    return bgiew;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
-(void)changeCHT:(UIButton *)sender{
    
    [SEUserData sharedInstance].user.languageStr = @"CHT";
    [[SEUserData sharedInstance] saveAccount];
    [_EBt removeFromSuperview];
    [_CBt removeFromSuperview];
    [SEChangeLanguage setUserlanguage:@"zh-Hans"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLanguage" object:self];
    [_tableView reloadData];
}
-(void)changeENG:(UIButton *)sender{
    
    [SEUserData sharedInstance].user.languageStr = @"ENG";
    [[SEUserData sharedInstance] saveAccount];
    [_EBt removeFromSuperview];
    [_CBt removeFromSuperview];
    [SEChangeLanguage setUserlanguage:@"en"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLanguage" object:self];
    [_tableView reloadData];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
